<template>
  <el-dialog
    title="审核弹窗"
    :visible="examineDialogVisible"
    @update:visible="updateExamineDialogVisibility"
    width="600px"
    :before-close="handleCancel"
    :close-on-click-modal="false"
  >
    <el-form
      ref="examineForm"
      :model="form"
      label-width="auto"
      label-position="left"
      :rules="rules"
    >
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择审核状态">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="form.status == '0' && type != 'activity'"
        label="原因"
        prop="rejectReason"
      >
        <el-input type="textarea" v-model="form.rejectReason" placeholder="请输入审核不通过原因" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { articleUpdateStatus } from '@/api/contentList'
import { activityUpdateStatus } from '@/api/activity'
export default {
  name: 'ExamineDialog',
  props: {
    examineDialogVisible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      id: '',
      form: {
        status: '',
        rejectReason: ''
      }
    }
  },
  computed: {
    rules() {
      return {
        status: [
          {
            required: true,
            message: '请选择审核状态',
            trigger: 'change'
          }
        ],
        rejectReason: [
          {
            required: this.form.status === '0' && this.type !== 'activity',
            message: '请输入审核不通过原因',
            trigger: 'blur'
          }
        ]
      }
    },
    statusOptions() {
      return [
        {
          label: '审核通过',
          value: this.type != 'activity' ? '3' : '2'
        },
        {
          label: '审核不通过',
          value: '0'
        }
      ]
    }
  },
  methods: {
    /**
     * @description: 接收从父组件传递过来的值
     * @param {*} obj
     * @return {*}
     */
    deepClone(obj) {
      this.id = obj.id
    },
    /**
     * @description: 弹窗取消事件
     * @return {*}
     */
    handleCancel() {
      this.$emit('cancel')
      this.id = ''
      this.form = {
        status: '',
        rejectReason: ''
      }
      this.$refs.form.resetFields()
    },
    /**
     * @description: 弹窗确事件
     * @return {*}
     */
    handleConfirm() {
      this.$refs.examineForm.validate((valid) => {
        if (valid) {
          this.updateStatus(this.form)
        }
      })
    },
    /**
     * @description: 新增
     * @param {*} data
     * @return {*}
     */
    updateStatus(data) {
      if (this.type == 'activity') {
        activityUpdateStatus({ ...data, id: this.id }).then((res) => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '审核成功'
            })
            this.form = {
              status: ''
            }
            this.$emit('confirm')
          }
        })
      } else {
        articleUpdateStatus({ ...data, id: this.id }).then((res) => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '审核成功'
            })
            this.form = {
              status: '',
              rejectReason: ''
            }
            this.$emit('confirm')
          }
        })
      }
    },
    updateExamineDialogVisibility(value) {
      this.$emit('update:examineDialogVisible', value)
    }
  }
}
</script>

<style></style>
